package cn.wsalix.biz.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.CommonRepository;
import cn.anlaser.status.FlagEnum;
import cn.wsalix.biz.entity.Category;
import cn.wsalix.biz.entity.RelationCat;
import cn.wsalix.biz.type.CatTypeEnum;

public interface RelationCatRepository extends CommonRepository<RelationCat> {
	@Query("select cat from RelationCat relaCat,Category cat where relaCat.category.id=cat.id and relaCat.fkId=:fkId and relaCat.catType = :catType")
	public List<Category> findByTypeAndFkid(@Param("fkId") Long fkId,
			@Param("catType") CatTypeEnum catType);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationCat relaCat where relaCat.category.id =:catId")
	public void deleteByCatId(@Param("catId") Long catId);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationCat relaCat where relaCat.fkId=:fkId and relaCat.catType = :catType")
	public void deleteByCatTypeAndFkid(@Param("fkId") Long fkId,
			@Param("catType") int catType);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationCat relaCat where relaCat.fkId=:fkId and relaCat.category.id=:catId and relaCat.catType = :catType")
	public void deleteByCatTypeAndFkid(@Param("fkId") Long fkId,
			@Param("catId") Long catId, @Param("catType") CatTypeEnum catType);

	@Query("select cat from RelationCat relaCat,Category cat where relaCat.category.id=cat.id and relaCat.fkId=:fkId and relaCat.catType = :catType and cat.flag= :flag and cat.createdBy=:createdBy")
	public List<Category> findByShopId(@Param("fkId") Long fkId, @Param("catType") CatTypeEnum catType,
			@Param("createdBy") Long createdBy, @Param("flag") FlagEnum flag);

}
